<?php
namespace app\api\controller\wsgoods;
use x\my\KdApi;

class Ordergoodsno extends Base{

    public function __construct() {
        parent::__construct();
        $this->checkLogin();
    }

    //订单列表
    public function lists(){
        $d = $this->post;
        $rule = [
            'page'=>'require|integer',
            'limit'=>'require|integer',
        ];
        $msg = [
            'page.require' => 'page不能为空',
            'limit.require' => 'limit不能为空',
        ];
        check($rule,$msg,$d);

        $map = [];
        $map[] = ["and","user_id","=",$this->userinfo["id"]];
        if($d['status']!='' && $d['status']!=0 ) $map[] = ['and',"status",'=',$d['status']];

        $w = where($map);
        $rs = $this->db->query("select * from order_goods_no {$w[0]} order by id desc".limit($d['page'],$d['limit']),$w[1],'',true);

        #订单编号
        $order_goods_nos = [];
        $order_status = [];
        $order_total_money = [];
        foreach ($rs['0'] as $v) {
            $order_goods_nos[] = $v['order_no'];
            $order_status[$v['order_no']] = $v['status'];
            $order_total_money[$v['order_no']] = $v['order_total_money'];
        }
        if (!$order_goods_nos) suc();
        #订单列表编组
        $field = [
            'id',
            'goods_price_id',
            'goods_id',
            'order_no',
            'title',
            'goods_price_title',
            'pic',
            'goods_price_price',
            'nums',
            'total_money',
            'use_points',
            'create_time'
        ];
        $field = implode(",", $field);
        $order_goods = $this->db->query("select {$field} from order_goods where user_id=? and order_no in(".implode(',',$order_goods_nos).") order by id desc",[$this->userinfo['id']]);
        $temp = [];
        $temp_nums = [];
        foreach ($order_goods_nos as $v) {
            $ordernums = 0;
            foreach ($order_goods as $val) {
                if ($v==$val['order_no']) {
                    if (!isset($temp[$v])) {
                        $temp[$v] = [];
                    }
                    $val['orderstatus'] = $order_status[$val['order_no']];
                    $val['create_time_name'] = com_time_format($val["create_time"]);
                    $val['order_total_money'] = $order_total_money[$val['order_no']];
                    $temp[$v][] = $val;
                    $ordernums += $val['nums'];
                }
            }
            if (!isset($temp_nums[$v])) $temp_nums[$v] = "";
            $temp_nums[$v] = $ordernums;
        }
        $temp_data = [];
        foreach ($temp as $k=>$v) {
            //$v['ordernums'] = $temp_nums[$k];
            $temp_data[] = $v;
        }
        suc($temp_data,$rs[1]);
    }

    #物流
    public function wuliu(){
        $d = $this->post;
        $rule = [
            'order_no'=>'require',
        ];
        $msg = [
            'order_no.require' => '订单编号不能为空',
        ];
        check($rule,$msg,$d);

        #订单列表
        $order_goods = $this->db->query("select * from order_goods where fahuo_status=1 and user_id=? and order_no=? order by id desc",[$this->userinfo['id'],$d['order_no']]);
        if (!$order_goods) suc();

        $temp = [];
        foreach ($order_goods as $v) {
            if (!isset($temp[$v['kd_no']])) {
                $temp[$v['kd_no']]['kd_name'] = $v['kd_name'];
                $temp[$v['kd_no']]['kd_no'] = $v['kd_no'];
                $wuliu = KdApi::getOrderTracesByJson($v['kd_code'],$v['kd_no']);
                $wuliu = json_decode($wuliu,true);
                if ($wuliu && $wuliu['Success']==true) {
                    $wuliu['Traces'] = array_reverse($wuliu['Traces']);
                }
                $temp[$v['kd_no']]['wuliu'] = $wuliu;
                $temp[$v['kd_no']]['list'] = [];
            }
            $temp[$v['kd_no']]['list'][] = $v;
        }
        $temp_data = [];
        foreach ($temp as $v) {
            $temp_data[] = $v;
        }
        suc($temp_data);

    }
}
?>
